//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.gui.fixed;

import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

public class RadioButtomAndComboBox extends JPanel {

    public RadioButtomAndComboBox() {
        jRadioButton = null;
        jComboBox = null;
        selectedString = "";
        selectedIndex = -1;
        jPanel = null;
        initialize();
    }

    private void initialize() {
        setLayout(new BorderLayout());
        setSize(306, 27);
        add(getJPanel(), "West");
    }

    private JRadioButton getJRadioButton() {
        if (jRadioButton == null) {
            jRadioButton = new JRadioButton();
            jRadioButton.setText("N/A");
            jRadioButton.setPreferredSize(new Dimension(150, 23));
            jRadioButton.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    if (jRadioButton.isSelected()) {
                        getJComboBox().setEnabled(true);
                    } else {
                        getJComboBox().setEnabled(false);
                    }
                }
            });
        }
        return jRadioButton;
    }

    private JComboBox getJComboBox() {
        if (jComboBox == null) {
            jComboBox = new JComboBox();
            jComboBox.setModel(new DefaultComboBoxModel(list));
            jComboBox.setEnabled(false);
            jComboBox.setPreferredSize(new Dimension(150, 23));
            jComboBox.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    String oldString = selectedString;
                    int oldIndex = selectedIndex;
                    selectedIndex = jComboBox.getSelectedIndex();
                    selectedString = (String) jComboBox.getSelectedItem();
                    firePropertyChange("selectedIndex", oldIndex, selectedIndex);
                    firePropertyChange("selectedString", oldString, selectedString);
                }
            });
        }
        return jComboBox;
    }

    private JRadioButton getRadioButton() {
        return getJRadioButton();
    }

    public void setLabel(String txt) {
        getJRadioButton().setText(txt);
    }

    public String getLabel() {
        return getJRadioButton().getText();
    }

    public String[] getList() {
        return list;
    }

    public void setList(String list[]) {
        String old[] = list;
        this.list = list;
        firePropertyChange("list", old, list);
        jComboBox.setModel(new DefaultComboBoxModel(list));
    }

    public int getSelectedIndex() {
        return selectedIndex;
    }

    public void setSelectedIndex(int selectedIndex) {
        int old = selectedIndex;
        this.selectedIndex = selectedIndex;
        firePropertyChange("selectedIndex", old, selectedIndex);
        jComboBox.setSelectedIndex(selectedIndex);
    }

    public String getSelectedString() {
        return selectedString;
    }

    public void setSelectedString(String selectedString) {
        String old = selectedString;
        this.selectedString = selectedString;
        firePropertyChange("selectedString", old, selectedString);
        jComboBox.setSelectedItem(selectedString);
    }

    private JPanel getJPanel() {
        if (jPanel == null) {
            jPanel = new JPanel();
            FlowLayout flowLayout7 = new FlowLayout();
            jPanel.setLayout(flowLayout7);
            flowLayout7.setHgap(2);
            flowLayout7.setVgap(2);
            jPanel.add(getJRadioButton(), null);
            jPanel.add(getJComboBox(), null);
        }
        return jPanel;
    }
    private JRadioButton jRadioButton;
    private JComboBox jComboBox;
    private String list[] = {
        "N/A"
    };
    private String selectedString;
    private int selectedIndex;
    private JPanel jPanel;
}
